#include "PrintSettingView.h"
#include "ui_PrintSettingForm.h"
#include <QDebug>


PrintSettingView::PrintSettingView(QWidget *parent) :
    QWidget(parent), ui(new Ui::PrintSetting)
{
    ui->setupUi(this);
    this->setWindowFlags(Qt::FramelessWindowHint);

    InitUi();

    curdevIndex = 0;
    curPaperIndex = 0;
    curformIndex = 0;

    m_settingHelper = SettingHelper::getSettingHelper();
    readSetting();

    connect(ui->comboBox_selDev,SIGNAL(currentIndexChanged(int)),this,SLOT(slot_devChanged(int)));
    connect(ui->comboBox_selPaper, SIGNAL(currentIndexChanged(int)),this,SLOT(slot_paperChanged(int)));
    connect(ui->comboBox_form, SIGNAL(currentIndexChanged(int)),this,SLOT(slot_formChanged(int)));

}

PrintSettingView::~PrintSettingView()
{
    delete ui;
}


void PrintSettingView::closeEvent(QCloseEvent *e)
{
    Q_UNUSED(e);
    writeSetting();
}

void PrintSettingView::slot_devChanged(int index)
{
    curdevIndex = index;
    if(index == 0)
    {
        ui->comboBox_selDrive->setEnabled(false);
        ui->comboBox_selPaper->setEnabled(false);
        ui->nu_lineEdit_copies->setEnabled(false);

        selForm.clear();
        ui->comboBox_form->clear();
        selForm.append(tr("横排带图"));
        selForm.append(tr("横排不带图"));
        selForm.append(tr("竖排带图"));
        selForm.append(tr("竖排不带图"));
        ui->comboBox_form->addItems(selForm);
        ui->comboBox_form->setCurrentIndex(0);

        selLanguage.clear();
        ui->comboBox_selLanguage->clear();
        selLanguage.append(tr("中文"));
        selLanguage.append(tr("英文缩写"));
        ui->comboBox_selLanguage->addItems(selLanguage);
        ui->comboBox_selLanguage->setCurrentIndex(0);
    }
    else
    {
        ui->comboBox_selPaper->setEnabled(true);
        ui->nu_lineEdit_copies->setEnabled(true);
        ui->comboBox_selDrive->setEnabled(true);

        selLanguage.clear();
        ui->comboBox_selLanguage->clear();
        selLanguage.append(tr("中文"));
        selLanguage.append(tr("中英文"));
        ui->comboBox_selLanguage->addItems(selLanguage);
        ui->comboBox_selLanguage->setCurrentIndex(0);
    }
}

void PrintSettingView::slot_paperChanged(int index)
{
    curPaperIndex = index;
    if(index == 0)
    {
        selForm.clear();
        ui->comboBox_form->clear();
        selForm.append(tr("整页带图"));
        selForm.append(tr("整页不带图"));
        ui->comboBox_form->addItems(selForm);
        ui->comboBox_form->setCurrentIndex(0);

        selLanguage.clear();
        ui->comboBox_selLanguage->clear();
        selLanguage.append(tr("中文"));
        ui->comboBox_selLanguage->addItems(selLanguage);
        ui->comboBox_selLanguage->setCurrentIndex(0);
    }
    else
    {
        selForm.clear();
        ui->comboBox_form->clear();
        selForm.append(tr("整页带图"));
        selForm.append(tr("整页不带图"));
        selForm.append(tr("半页带图"));
        selForm.append(tr("半页不带图"));
        ui->comboBox_form->addItems(selForm);
        ui->comboBox_form->setCurrentIndex(0);

        selLanguage.clear();
        ui->comboBox_selLanguage->clear();
        selLanguage.append(tr("中文"));
        selLanguage.append(tr("中英文"));
        ui->comboBox_selLanguage->addItems(selLanguage);
        ui->comboBox_selLanguage->setCurrentIndex(0);
    }
}

void PrintSettingView::slot_formChanged(int index)
{

    if(curdevIndex == 0)
    {
        if(index == 0 || index == 1)
        {
            selLanguage.clear();
            ui->comboBox_selLanguage->clear();
            selLanguage.append(tr("中文"));
            selLanguage.append(tr("英文缩写"));
            ui->comboBox_selLanguage->addItems(selLanguage);
            ui->comboBox_selLanguage->setCurrentIndex(0);
        }
        else
        {
            selLanguage.clear();
            ui->comboBox_selLanguage->clear();
            selLanguage.append(tr("英文缩写"));
            ui->comboBox_selLanguage->addItems(selLanguage);
            ui->comboBox_selLanguage->setCurrentIndex(0);
        }
    }
    else
    {
        if(curPaperIndex != 0)
        {
            if(index == 0 || index == 1)
            {
                selLanguage.clear();
                ui->comboBox_selLanguage->clear();
                selLanguage.append(tr("中文"));
                selLanguage.append(tr("中英文"));
                ui->comboBox_selLanguage->addItems(selLanguage);
                ui->comboBox_selLanguage->setCurrentIndex(0);
            }
            else
            {
                selLanguage.clear();
                ui->comboBox_selLanguage->clear();
                selLanguage.append(tr("中文"));
                ui->comboBox_selLanguage->addItems(selLanguage);
                ui->comboBox_selLanguage->setCurrentIndex(0);
            }
        }
    }

}

void PrintSettingView::InitUi()
{
    QValidator *validator = new QIntValidator(1, 20, ui->nu_lineEdit_copies );
    ui->nu_lineEdit_copies->setValidator( validator );
}

void PrintSettingView::readSetting()
{
    m_settingHelper->printReadSetting();//读取配置文件中的内容到打印结构体

//    ui->comboBox_selDev->setEditText(m_settingHelper->m_PrintSet.selDev);
//    ui->comboBox_selDrive->setEditText(m_settingHelper->m_PrintSet.selDrive);
//    ui->comboBox_selPaper->setEditText(m_settingHelper->m_PrintSet.selPaper);
//    ui->ch_lineEdit_title->setText(m_settingHelper->m_PrintSet.title);
//    ui->comboBox_form->setEditText(m_settingHelper->m_PrintSet.form);
//    ui->comboBox_selLanguage->setEditText(m_settingHelper->m_PrintSet.language);
//    ui->nu_lineEdit_copies->setText(m_settingHelper->m_PrintSet.copies);

    ui->checkBox1->setChecked(m_settingHelper->m_PrintSet.printFlag[0]);
    ui->checkBox2->setChecked(m_settingHelper->m_PrintSet.printFlag[1]);
    ui->checkBox3->setChecked(m_settingHelper->m_PrintSet.printFlag[2]);
    ui->checkBox4->setChecked(m_settingHelper->m_PrintSet.printFlag[3]);
    ui->checkBox5->setChecked(m_settingHelper->m_PrintSet.printFlag[4]);

    switch(m_settingHelper->m_PrintSet.autoPrint)
    {
    case 0:
        ui->radioButton1->setChecked(true);
        break;
    case 1:
        ui->radioButton2->setChecked(true);
        break;
    case 2:
        ui->radioButton3->setChecked(true);
        break;
    }
}

void PrintSettingView::writeSetting()
{
//    m_settingHelper->m_PrintSet.selDev = ui->comboBox_selDev->currentText();
//    m_settingHelper->m_PrintSet.selDrive = ui->comboBox_selDrive->currentText();
//    m_settingHelper->m_PrintSet.selPaper = ui->comboBox_selPaper->currentText();
//    m_settingHelper->m_PrintSet.title = ui->ch_lineEdit_title->text();
//    m_settingHelper->m_PrintSet.form = ui->comboBox_form->currentText();
//    m_settingHelper->m_PrintSet.language = ui->comboBox_selLanguage->currentText();
//    m_settingHelper->m_PrintSet.copies = ui->nu_lineEdit_copies->text();

    m_settingHelper->m_PrintSet.printFlag[0] = ui->checkBox1->isChecked();
    m_settingHelper->m_PrintSet.printFlag[1] = ui->checkBox2->isChecked();
    m_settingHelper->m_PrintSet.printFlag[2] = ui->checkBox3->isChecked();
    m_settingHelper->m_PrintSet.printFlag[3] = ui->checkBox4->isChecked();
    m_settingHelper->m_PrintSet.printFlag[4] = ui->checkBox5->isChecked();

    if(ui->radioButton1->isChecked())
    {
        m_settingHelper->m_PrintSet.autoPrint = 0;
    }
    else if(ui->radioButton2->isChecked())
    {
        m_settingHelper->m_PrintSet.autoPrint = 1;
    }
    else
    {
        m_settingHelper->m_PrintSet.autoPrint = 2;
    }


    m_settingHelper->printWriteSetting();     //将设置内容写入配置文件
}
